package rentadomus

class LoginController {
    
    def index = {
        if(session.user) {
            redirect(uri="/")
        }else{
            redirect(action:"login")
        }
    }
    
    def login = {}
    
    def authenticate(){
        Person person = Person.findByUsernameAndPassword(params.username, params.password)
        if(person){
            session.person = person
            flash.message = "Welcome ${person.name}!"
            redirect(uri:"/")
        }else{
            flash.message = "Sorry, ${params.username}. Invalid username or password."
            redirect(action:"login")
        }
    }
    
    def logout = {
        flash.message = "Bye ${session.person.name}"
        session.person = null
        redirect(uri:"/")
    }
}